home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / Array3.h < prev    next >
C/C++ Source or Header  |  1996-10-30  |  4KB  |  168 lines

  1. // Template array classes
  2. /*
  3.  
  4. Copyright (C) 1996 John W. Eaton
  5.  
  6. This file is part of Octave.
  7.  
  8. Octave is free software; you can redistribute it and/or modify it
  9. under the terms of the GNU General Public License as published by the
  10. Free Software Foundation; either version 2, or (at your option) any
  11. later version.
  12.  
  13. Octave is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with Octave; see the file COPYING.  If not, write to the Free
  20. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22. */
  23.  
  24. #if !defined (octave_Array3_h)
  25. #define octave_Array3_h 1
  26.  
  27. #if defined (__GNUG__)
  28. #pragma interface
  29. #endif
  30.  
  31. #include <cassert>
  32. #include <cstdlib>
  33.  
  34. #include "Array2.h"
  35. #include "lo-error.h"
  36.  
  37. class idx_vector;
  38.  
  39. // Three dimensional array class.
  40.  
  41. template <class T>
  42. class Array3 : public Array2<T>
  43. {
  44. protected:
  45.  
  46.   int d3;
  47.  
  48.   Array3 (T *d, int n, int m, int k) : Array2<T> (d, n, m*k)
  49.     {
  50.       d2 = m;
  51.       d3 = k;
  52.       set_max_indices (3);
  53.     }
  54.  
  55. public:
  56.  
  57.   Array3 (void) : Array2<T> ()
  58.     {
  59.       d2 = 0;
  60.       d3 = 0;
  61.       set_max_indices (3);
  62.     }
  63.  
  64.   Array3 (int n, int m, int k) : Array2<T> (n, m*k)
  65.     {
  66.       d2 = m;
  67.       d3 = k;
  68.       set_max_indices (3);
  69.     }
  70.  
  71.   Array3 (int n, int m, int k, const T& val) : Array2<T> (n, m*k, val)
  72.     {
  73.       d2 = m;
  74.       d3 = k;
  75.       set_max_indices (3);
  76.     }
  77.  
  78.   Array3 (const Array3<T>& a) : Array2<T> (a)
  79.     {
  80.       d2 = a.d2;
  81.       d3 = a.d3;
  82.       set_max_indices (3);
  83.     }
  84.  
  85.   ~Array3 (void) { }
  86.  
  87.   Array3<T>& operator = (const Array3<T>& a)
  88.     {
  89.       if (this != &a && rep != a.rep)
  90.     {
  91.       Array<T>::operator = (a);
  92.       d1 = a.d1;
  93.       d2 = a.d2;
  94.       d3 = a.d3;
  95.     }
  96.  
  97.       return *this;
  98.     }
  99.  
  100.   int dim3 (void) const { return d3; }
  101.  
  102.   // No checking of any kind, ever.
  103.  
  104.   T& xelem (int i, int j, int k) { return Array2<T>::xelem (i, d2*k+j); }
  105.   T xelem (int i, int j, int k) const { return Array2<T>::xelem (i, d2*k+j); }
  106.  
  107.   // Note that the following element selection methods don't use
  108.   // xelem() because they need to make use of the code in
  109.   // Array<T>::elem() that checks the reference count.
  110.  
  111.   T& checkelem (int i, int j, int k)
  112.     {
  113.       if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3)
  114.     {
  115.       (*current_liboctave_error_handler) ("range error");
  116.       static T foo;
  117.       return foo;
  118.     }
  119.       return Array2<T>::elem (i, d2*k+j);
  120.     }
  121.  
  122.   T& elem (int i, int j, int k) { return Array2<T>::elem (i, d2*k+j); }
  123.  
  124. #if defined (BOUNDS_CHECKING)
  125.   T& operator () (int i, int j, int k) { return checkelem (i, j, k); }
  126. #else
  127.   T& operator () (int i, int j, int k) { return elem (i, j, k); }
  128. #endif
  129.  
  130.   T checkelem (int i, int j, int k) const
  131.     {
  132.       if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3)
  133.     {
  134.       (*current_liboctave_error_handler) ("range error");
  135.       return T ();
  136.     }
  137.       return Array2<T>::elem (i, d1*k+j);
  138.     }
  139.  
  140.   T elem (int i, int j, int k) const { return Array2<T>::elem (i, d2*k+j); }
  141.  
  142. #if defined (BOUNDS_CHECKING)
  143.   T operator () (int i, int j, int k) const { return checkelem (i, j, k); }
  144. #else
  145.   T operator () (int i, int j, int k) const { return elem (i, j, k); }
  146. #endif
  147.  
  148.   void resize (int n, int m, int k);
  149.   void resize (int n, int m, int k, const T& val);
  150.  
  151. #ifdef HEAVYWEIGHT_INDEXING
  152.   void maybe_delete_elements (idx_vector& i, idx_vector& j, idx_vector& k);
  153.  
  154.   Array3<T> value (void);
  155. #endif
  156. };
  157.  
  158. template <class LT, class RT>
  159. int assign (Array3<LT>& lhs, const Array3<RT>& rhs);
  160.  
  161. #endif
  162.  
  163. /*
  164. ;;; Local Variables: ***
  165. ;;; mode: C++ ***
  166. ;;; End: ***
  167. */
  168.